home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14508 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: nwlink.com!usenet
  2. From: Teresa Reiko <tjr19@mail.nwlink.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Check if a file exists?
  5. Date: 15 Apr 1996 14:05:52 GMT
  6. Organization: Northwest Link
  7. Message-ID: <4ktl40$b76@texas.nwlink.com>
  8. References: <4kp7pg$upe@news-s01.ny.us.ibm.net>
  9. NNTP-Posting-Host: port55.annex2.nwlink.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.2 (Windows; U; 16bit)
  14.  
  15. bfilone@ibm.net  <Bruce Filone> wrote:
  16. >Is it possible in C to check if a file exists other than checking for a >succesful 
  17. >fopen? ON a unix machine, I just want to know if a file is there or not, >do not
  18. >to read or write anything to it. similar to the -f test in unix? I was >hoping for
  19. >something less costly than opening and closing if exists because I am >checking
  20. >for the existance of many files during the run of a process 
  21. >Did not see any postings that seemed to refer to this.
  22.  
  23. Use rename():
  24.  
  25. You could try to rename it, and if that fails it's not there,
  26. if it works it exists, and then you can rename it back.
  27.  
  28. Or, you can create a temporary file, try to rename it to the
  29. name of the file you're testing, if it fails it exists, and
  30. if it works it wasn't there, and then you can rename it back.
  31.  
  32. This should be faster than fopen, but you would need to have
  33. write access to all the files you're testing.
  34.  
  35. ----- for every vision there is an equal and opposite revision ... -----
  36. ------------------------------------------------------------------------
  37.  Teresa Reiko    Chief Programmer, Tenbyte Software    tjr19@nwlink.com
  38. ------------------------------------------------------------------------
  39.  
  40.  
  41.